package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    @Transactional
    public void status(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        //如果是三级菜单直接删除
        //delete frpm parent_id where id =#{id}
        if (itemCat.getLevel() == 3){
            itemCatMapper.deleteById(itemCat);
        }
        //如果是二级菜单，通过二级id找到三级，先删除三级  再删除二级
        //delete from item_cat where parent_id = #{id} or id = #{id}
        if (itemCat.getLevel() == 2){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", itemCat.getId())
                        .or()
                        .eq("id", itemCat.getId());
            itemCatMapper.delete(queryWrapper);
        }
        //如果是一级菜单，首先获取二级菜单，再根据二级菜单获取三级菜单，然后再一一删除
        //delete from item_cat parent_id in (twoIds) or parent_id = #{id} or id = #{id}
        if (itemCat.getLevel() == 1){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", itemCat.getId());
            List twoIds = itemCatMapper.selectObjs(queryWrapper);
            queryWrapper.clear();
            queryWrapper.in(twoIds.size()>0,"parent_id", twoIds)
                        .or()
                        .in(twoIds.size()>0,"id", twoIds)
                        .or()
                        .eq("id", itemCat.getId());
            itemCatMapper.delete(queryWrapper);
        }
    }
    //把所有数据封装到map集合中

    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        List<ItemCat> itemCatList = itemCatMapper.selectList(null);
        for (ItemCat itemCat : itemCatList){
            Integer key = itemCat.getParentId();
            if (map.containsKey(key)){
                map.get(key).add(itemCat);
            }else {
                List<ItemCat> list = new ArrayList<>();
                list.add(itemCat);
                map.put(key,list);
            }
        }
        return map;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        Map<Integer, List<ItemCat>> map = getMap();
        //只获取一级菜单
        if (level == 1){
            return map.get(0);
        }
        //获取一级和二级
        if (level == 2){
            return getTwoList(map);
        }
        List<ItemCat> oneList = getThreeList(map);
        return oneList;
    }

    //获取三级列表 先获取1级  在2 再3
    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map){
        List<ItemCat> oneList = getTwoList(map);
        for (ItemCat oneItemCat : oneList){
            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList == null || twoList.size() == 0){
                continue;
            }
            for (ItemCat twoItemCat : twoList){
                Integer twoId = twoItemCat.getId();
                List<ItemCat> threeList = map.get(twoId);
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    //获取一级和二级菜单
    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map){
        List<ItemCat> oneList =  map.get(0);
        for (ItemCat oneItemCat : oneList){
            Integer oneId = oneItemCat.getId();
            List<ItemCat> twoList = map.get(oneId);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }

   /* @Override
    public List<ItemCat> findItemCatList(Integer level) {
        //一级菜单列表
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<ItemCat> oneList = itemCatMapper.selectList(queryWrapper);
        //二级菜单列表
        for (ItemCat oneItemCat : oneList){
            Integer oneId = oneItemCat.getId();
            queryWrapper.clear();
            queryWrapper.eq("parent_id",oneId);
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for (ItemCat twoItemCat : twoList){
                Integer twoId = twoItemCat.getId();
                queryWrapper.clear();
                queryWrapper.eq("parent_id",twoId);
                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
                twoItemCat.setChildren(threeList);
            }
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }*/
}
